<template>
    <!-- pages/setUserInfo/setUserInfo.wxml -->
    <view class="container">
        <view class="photo">
            <view class="img"><image :src="userinfo.avatar ? userinfo.avatar : '/logo.png'" mode="widthFix" /></view>
            <button hover-class="button-click" class="tip" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">点击更新头像</button>
        </view>
        <view class="list">
            <view class="item" @tap="setUserName">
                <view class="left">
                    <view class="label">昵称</view>
                    <text>{{ userinfo.nickname }}</text>
                </view>
                <view class="iconfont icon-right"></view>
            </view>
            <view class="item">
                <view class="left">
                    <view class="label">手机</view>
                    <text>{{ userinfo.mobile }}</text>
                </view>
                <!-- <view class="btn" bindtap="setPhone">更换手机</view> -->
            </view>
        </view>
        <view :class="'bottom ' + (isIpx ? 'fix-iphonex-button' : '')">
            <view class="btn theme-color-shadow theme-bg-primary" @tap="exitLogin" style="color: #fff">退出登录</view>
        </view>
    </view>
</template>

<script>
// pages/setUserInfo/setUserInfo.js
var http = require('../../utils/http');
var util1 = require('../../utils/util.js');
const app = getApp();
export default {
    data() {
        return {
            userinfo: {
                avatar: false,
                nickname: '',
                mobile: ''
            },

            //用户信息
            isIpx: app.globalData.isIpx ? true : false,

            photoShow: false
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad(options) {},
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getuserinfo();
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {},
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {},
    methods: {
        // 获取用户信息
        getuserinfo: function () {
            var that = this;
            if (app.globalData.isLogin) {
                http.request(
                    '/member/user/get',
                    '1',
                    'get',
                    {},
                    app.globalData.userDatatoken.accessToken,
                    '',
                    function success(info) {
                        // console.info('我的信息===');
                        // console.info(info);
                        if (info.code == 0) {
                            that.setData({
                                userinfo: info.data
                            });
                        }
                    },
                    function fail(info) {}
                );
            } else {
                //console.log('未登录失败！')
            }
        },

        closeUserInfo() {
            this.setData({
                photoShow: false
            });
        },

        // 修改昵称
        setUserName: function () {
            let name = this.userinfo.nickname ? this.userinfo.nickname : '';
            uni.navigateTo({
                url: '../setUserName/setUserName?name=' + name
            });
        },

        // 修改手机号
        setPhone: function () {
            let that = this;
            let phone = this.userinfo.mobile ? this.userinfo.mobile : '';
            uni.navigateTo({
                url: '../setUserPhone/setUserPhone?phone=' + phone
            });
        },

        // 选择头像
        onChooseAvatar(e) {
            const { avatarUrl } = e.detail;
            //console.log(avatarUrl);
            var that = this;
            uni.uploadFile({
                url: app.globalData.baseUrl + '/member/store/uploadImg',
                filePath: avatarUrl,
                name: 'file',
                header: {
                    'tenant-id': app.globalData.tenantId,
                    'Content-Type': 'application/json',
                    Authorization: 'Bearer ' + app.globalData.userDatatoken.accessToken
                },
                success(res) {
                    var data = JSON.parse(res.data);
                    that.updateAvatar(data.data);
                }
            });
        },

        // 修改头像
        updateAvatar: function (avatarUrl) {
            var that = this;
            if (app.globalData.isLogin) {
                http.request(
                    '/member/user/updateAvatar?avatarUrl=' + avatarUrl,
                    '1',
                    'post',
                    {
                        avatarUrl: avatarUrl
                    },
                    app.globalData.userDatatoken.accessToken,
                    '',
                    function success(info) {
                        if (info.code == 0) {
                            uni.showToast({
                                title: '修改成功',
                                icon: 'success'
                            });
                            that.getuserinfo();
                        }
                    },
                    function fail(info) {}
                );
            } else {
                //console.log('未登录失败！')
            }
        },

        // 退出登录
        exitLogin: function () {
            var that = this;
            if (app.globalData.isLogin) {
                http.request(
                    '/member/auth/logout',
                    '1',
                    'post',
                    {},
                    app.globalData.userDatatoken.accessToken,
                    '加载中...',
                    function success(info) {
                        if (info.code == 0) {
                            uni.showToast({
                                title: '已退出',
                                icon: 'success'
                            });
                            app.globalData.userData = {};
                            uni.setStorageSync('userDatatoken', '');
                            app.globalData.isLogin = false;
                            app.globalData.userDatatoken = {};
                            uni.navigateBack();
                        }
                    },
                    function fail(info) {}
                );
            } else {
                //console.log('未登录失败！')
            }
        }
    }
};
</script>
<style>
@import './setUserInfo.css';
</style>
